package com.tanhua.server.service;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.RecommendUserQueryParam;
import com.tanhua.server.vo.TodayBest;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author 王加鑫
 * @Date 2021/09/03 23:01
 * @Since JDK 1.8
 */
@Service
public class RecommendationService {

    @Autowired
    private RecommendUserService recommendUserService;

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 根据用户token和param查询出推荐用户(集合)
     * @param
     * @param param
     * @return
     */
    public PageResult queryRecommendation(RecommendUserQueryParam param) {
        User user = UserThreadLocal.get();
        PageResult pageResult = new PageResult();
        pageResult.setPage(param.getPage());
        pageResult.setPagesize(param.getPagesize());
        //根据userId去mongodb中查询被推荐用户的数据的集合(缘分值)
        PageInfo<RecommendUser> pageInfo = recommendUserService.queryRecommendation(user.getId(), param.getPage(), param.getPagesize());
        List<RecommendUser> records = pageInfo.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return pageResult;
        }

        //遍历records获取userId集合(被推荐用户id)
        List<Object> ids = CollUtil.getFieldValues(records, "userId");
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",ids);
        //定义动态SQL
        /*if(StringUtils.isNotEmpty(param.getGender())){
            //需要性别参数查询
            queryWrapper.eq("sex",StringUtils.equals(param.getGender(),"man") ? 1:2);
        }
        if(StringUtils.isNotEmpty(param.getCity())){
            //需要城市参数查询
            queryWrapper.eq("city",param.getCity());
        }
        if(null != param.getAge()){
            //设置年龄参数，条件：小于等于
            queryWrapper.le("age",param.getAge());
        }*/
        //根据ids查询mysql数据库获得userInfoList集合
        List<UserInfo> userInfoList = userInfoService.queryUserInfoByList(queryWrapper);
        if(CollUtil.isEmpty(userInfoList)){
            return pageResult;
        }
        //将mongodb和mysql数据封装进todayBests中
        List<TodayBest> todayBests = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            TodayBest todayBest = new TodayBest();
            todayBest.setId(userInfo.getId());
            todayBest.setGender(userInfo.getSex().getValue() == 1 ? "man":"woman");
            todayBest.setAge(userInfo.getAge());
            todayBest.setAvatar(userInfo.getLogo());
            todayBest.setNickname(userInfo.getNickName());
            todayBest.setTags(StringUtils.split(userInfo.getTags(),","));
            for (RecommendUser record : records) {
                if(record.getUserId() == userInfo.getUserId()){
                    //获取缘分值，去除小数点转化成long类型放进todayBest对象中
                    todayBest.setFateValue(Double.valueOf(Math.floor(record.getScore())).longValue());
                }
            }
            todayBests.add(todayBest);

        }
        Collections.sort(todayBests,(o1, o2) -> new Long(o2.getFateValue() - o1.getFateValue()).intValue());
        pageResult.setItems(todayBests);
        return pageResult;
    }


}
